package com.demo;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

// wallet和什么最强相关：myUTXO
// 如何计算myUTXO：isSpent
public class WalletDialog extends JFrame {
    private List<Boolean> isSpent;
    private JList<String> incomeList;     // 收入列表
    private JList<String> expenseList;    // 支出列表
    private JList<String> utxoList;       // UTXO 列表
    private List<Transaction> myTxIn;
    private List<Transaction> myTxOut;
    private List<Transaction> myUTXO;
    private String myPubKey;
    private String mySigKey;
    public WalletDialog(Map<String, String> shareDataList, Chain myBlockChain, List<Boolean> isSpent) {
        this.myPubKey = Utils.getValueByKey(shareDataList, "publicKey");
        this.mySigKey = Utils.getValueByKey(shareDataList, "privateKey");
        this.myTxIn = Utils.findMyTxIn(myBlockChain,myPubKey);
        this.myTxOut = Utils.findMyTxOut(myBlockChain,myPubKey);
        this.isSpent = isSpent;
        this.myUTXO = new ArrayList<>();
        // 根据myTxIn和isSpent算出myUTXO
        if(!(isSpent.size() < myTxIn.size())) {
            getMyUTXO(); // isSpent的计算应该只在TxDialog中进行，myUTXO的计算可根据isSpent和myTxIn在任意位置计算（因为Dialog之间传递的是isSpent而不是myUTXO）
        }
        // 设置窗体的标题
        setTitle("钱包");
        // 设置窗体的大小
        setSize(400, 500);

        // test数据 List.of("支出1: 0.3 BTC", "支出2: 0.1 BTC");
        List<String> incomeStrList = new ArrayList<>();
        for(int i = 0; i<myTxIn.size(); i++){
            Transaction txIn = myTxIn.get(i);
            String txInStr = String.format("收入%d : %d   BTC",i+1,txIn.amount);
            incomeStrList.add(txInStr);
        }
        List<String> expenseStrList = new ArrayList<>();
        for(int i = 0; i<myTxOut.size(); i++){
            Transaction txOut = myTxOut.get(i);
            String txOutStr = String.format("支出%d : %d   BTC",i+1,txOut.amount);
            expenseStrList.add(txOutStr);
        }
        List<String> utxoStrList = new ArrayList<>();
        for(int i = 0; i<myUTXO.size(); i++){
            Transaction utxo = myUTXO.get(i);
            String utxoStr = String.format("utxo%d : %d   BTC",i+1,utxo.amount);
            utxoStrList.add(utxoStr);
        }

        // 初始化收入、支出、UTXO列表
        incomeList = new JList<>(incomeStrList.toArray(new String[0]));
        expenseList = new JList<>(expenseStrList.toArray(new String[0]));
        utxoList = new JList<>(utxoStrList.toArray(new String[0]));
        // 设置每个JList的背景色
        incomeList.setBackground(new Color(240, 240, 240));
        this.expenseList.setBackground(new Color(240, 240, 240));
        this.utxoList.setBackground(new Color(240, 240, 240));

        // 创建标签
        JLabel incomeLabel = new JLabel("收入   ");
        JLabel expenseLabel = new JLabel("支出   ");
        JLabel utxoLabel = new JLabel("UTXO");

        // 设置垂直布局容器
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // 垂直方向排列

        // 设置每一部分内容的布局
        JPanel incomePanel = new JPanel();
        incomePanel.setLayout(new FlowLayout(FlowLayout.LEFT)); // 左对齐
        incomePanel.add(incomeLabel);  // 收入标签
        JScrollPane incomeScrollPane = new JScrollPane(incomeList); // ScrollPane
        incomeScrollPane.setPreferredSize(new Dimension(300, 150));  // 设置宽300，高150
        incomePanel.add(incomeScrollPane);  // 收入列表

        JPanel expensePanel = new JPanel();
        expensePanel.setLayout(new FlowLayout(FlowLayout.LEFT)); // 左对齐
        expensePanel.add(expenseLabel);
        JScrollPane expenseScrollPane = new JScrollPane(this.expenseList); // ScrollPane
        expenseScrollPane.setPreferredSize(new Dimension(300, 150));  // 设置宽300，高150
        expensePanel.add(expenseScrollPane);

        JPanel utxoPanel = new JPanel();
        utxoPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); // 左对齐
        utxoPanel.add(utxoLabel);
        JScrollPane utxoScrollPane = new JScrollPane(this.utxoList); // ScrollPane
        utxoScrollPane.setPreferredSize(new Dimension(300, 150));  // 设置宽300，高150
        utxoPanel.add(utxoScrollPane);


        // 将内容面板添加到主面板
        panel.add(incomePanel);
        panel.add(expensePanel);
        panel.add(utxoPanel);

        // 将面板添加到窗体
        add(panel);
        setLocationRelativeTo(null);  // 窗体居中显示
    }

    private void getMyUTXO() {
        // 确保isSpent列表大小与myTxIn匹配
        while (isSpent.size() < myTxIn.size()) {
            isSpent.add(false);
        }
        for(int i = 0;i<myTxIn.size();i++){ // iSpent越界
            Boolean flag = isSpent.get(i);
            if(!flag){
                Transaction utxo = myTxIn.get(i);
                myUTXO.add(utxo);
            }
        }
    }

//    public static void main(String[] args) {
//        // 模拟数据
////        List<String> income = List.of("收入1: 0.5 BTC", "收入2: 1.2 BTC");
////        List<String> expense = List.of("支出1: 0.3 BTC", "支出2: 0.1 BTC");
////        List<String> utxo = List.of("UTXO1: 0.4 BTC", "UTXO2: 0.8 BTC");
//
//        // 在事件调度线程中启动应用程序
//        SwingUtilities.invokeLater(new Runnable() {
//            @Override
//            public void run() {
//                List<Map.Entry<String, String>> testList = new ArrayList<>(); // 空
//                Map.Entry<String,String> test1 = Map.entry("publicKey", "MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCCNIQ6cHSS/6VlOZbciZx0n+7GHGdAEitRcJmZ38OJj+Q==");
//                Map.Entry<String,String> test2 = Map.entry("privateKey", "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEUErt/vpIm0xbzEPuQvC5ZCx5PwOepVGzBZK5fRVQ3r4ROKZjFZbwOoLiW1wzhcbxBv7kNge3HWqY6VKlMLqGQw==");
//                testList.add(test1);
//                testList.add(test2);
//                new com.demo.WalletDialog(testList,new Chain(),new ArrayList<>()).setVisible(true);
//            }
//        });
//    }
}
